package com.nail.post.service;


import com.nail.post.Exception.UserUnAvtiveException;
import com.nail.post.entity.User;
import com.nail.post.service.LoginService;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;

import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;



public class ShiroRealm extends AuthenticatingRealm{

    @Autowired
    private LoginService loginService;

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
       //把AuthenticationToken转换为UsernamePasswordToken
        System.out.println("验证.....................");
        UsernamePasswordToken upToken = (UsernamePasswordToken) authenticationToken;

        //2.从UsernamePasswordToken中获取username
        String username= upToken.getUsername();
        //3.调用数据库的方法，从数据库中获取username对应的记录


        User user = loginService.getUserByUsername(username);



        //若用户不存在，抛出UnkownAccountException
        if (user==null){
            throw new UnknownAccountException("用户不存在");
        }
        if (user.getState()==false){
            throw new UserUnAvtiveException("该账号未激活！");
        }
        //5.根据用户信息，决定是否抛出其他的AuthenticationException
        //5.根据用户情况，来构建AuthenticationInfo对象并返回Name
       Object principle = username;
       Object credentials =  user.getPassword();
        String realmnName = getName();
        ByteSource credentialsSalt = ByteSource.Util.bytes(username);
        SimpleAuthenticationInfo simpleAuthenticationInfo = null;//new SimpleAuthenticationInfo(principle,credentials,realmnName);
        simpleAuthenticationInfo = new SimpleAuthenticationInfo(principle,credentials,credentialsSalt,realmnName);//盐值加密

        return simpleAuthenticationInfo;
    }

    public static void main(String[] args) {
        Object result = new SimpleHash("MD5","123",null,1);
        System.out.println(result);
    }
}
